/*************************************************
Author: will
Date: 8/24/2012
Description:
Notes: 
*************************************************/

#ifndef __CBattleRoundCtrl_h__
#define __CBattleRoundCtrl_h__

#include "CBattleRole.h"
#include "CBattleRules.h"
#include "CBattleActionCtrl.h"
#include "../common/CRankArray.h"

class CBattleRoundUnit
{
    friend class CBattleRoundCtrl;
public:
    CBattleRoundUnit() 
    {
        Clear();
    }

    void Clear()
    {
        _bLeft = false;
        _iRoleID = -1;
        _iOrder = 0;
        _iPos = 0;
    }

    virtual ~CBattleRoundUnit() 
    {
    }

    bool IsLeft() const {return _bLeft;}
    int GetRoleID() const {return _iRoleID;}
    int GetOrder() const {return _iOrder;}
    int GetPos() const {return _iPos;}

private:
    bool _bLeft;
    int _iRoleID;
    int _iOrder;
    int _iPos;
};

class CBattleRole;
class CBattleRolePareData;
class CBattleRoundRoleData
{
    friend class CBattleRolePareData;
    friend class CBattleRoundCtrl;
public:
    CBattleRoundRoleData() 
    {
        Clear();
    }

    void Clear()
    {
        _bHasRole = false;
        _iRoleID = -1;
        _iLastRoundID = -1;
    }

    virtual ~CBattleRoundRoleData() 
    {
    }

    int GetRoleID() const {return _iRoleID;}

    bool IsCanDoTurn(int iRoundNum, CBattleRoundRoleData& stRoundRoleData) 
    {
        if (!_bHasRole)
        {
            return false;
        }

        if (_iLastRoundID >= iRoundNum)
        {
            return false;
        }

        stRoundRoleData = *this;
        _iLastRoundID = iRoundNum;
        return true;
    }

    void DumpRoundRoleInfo();

private:
    bool _bHasRole;
    int _iRoleID;
    int _iLastRoundID;
};

class CBattleRolePareData
{
    friend class CBattleRoundCtrl;
public:
    CBattleRolePareData() 
    {
        Clear();
    }

    virtual ~CBattleRolePareData() 
    {
    }

    void Clear()
    {
        _bAfirst = false;
        _stRoleA.Clear();
        _stRoleB.Clear();
    }

    bool HasRole()
    {
        return _stRoleA._bHasRole || _stRoleB._bHasRole;
    }

    bool IsCanDoTurn(int iRoundNum, CBattleRoundRoleData& stRoundRoleData) 
    {
        if (!HasRole())
        {
            return false;
        }

        if (_bAfirst)
        {
            if (_stRoleA.IsCanDoTurn(iRoundNum, stRoundRoleData))
            {
                return true;
            }
            else if (_stRoleB.IsCanDoTurn(iRoundNum, stRoundRoleData))
            {
                return true;
            }
        }
        else
        {
            if (_stRoleB.IsCanDoTurn(iRoundNum, stRoundRoleData))
            {
                return true;
            }
            else if (_stRoleA.IsCanDoTurn(iRoundNum, stRoundRoleData))
            {
                return true;
            }
        }

        return false;
    }
    
    void DumpRolePareInfo();

private:
    bool _bAfirst;
    CBattleRoundRoleData _stRoleA;
    CBattleRoundRoleData _stRoleB;
};

class CBattleRoundCtrl
{
public:
    CBattleRoundCtrl() 
    {
        Clear();
    }

    virtual ~CBattleRoundCtrl() 
    {
    }

    void Clear()
    {
        _bIsCircleEnd = false;
        _bLastTurnIsLeft = false;
        _iRoundNum = 0;
        _astStatRank.Clear();

        //InitRoundCtrl前期使用此数组校验一个萝卜一个坑所以这样初始化
        _astRoleStats.m_iUsedCount = _astRoleStats.GetCount();
        for (int i = 0; i < _astRoleStats.Size(); ++i)
        {
            _astRoleStats[i].Clear();
        }
    }

    bool LastTurnIsLeft() { return _bLastTurnIsLeft;}
    int GetRoundNum() const {return _iRoundNum;}

    int NextTurn();
    int InitRoundCtrl();
    int InitAddRole(CBattleRole& stBattleRole);

    void DumpRoundCtrlInfo();

    bool IsLeftFirst() const
    {
        if (_astStatRank.Size() <= 0)
        {
            return false;
        }
        return _astStatRank[0]._bLeft;
    }

    bool IsCircleEnd() const {return _bIsCircleEnd;}
    void SetCircleEnd(bool bIsCircleEnd){_bIsCircleEnd = bIsCircleEnd;}

private:
    bool _bLastTurnIsLeft;
    bool _bIsCircleEnd;
    int _iRoundNum;
    CRankArray<CBattleRoundUnit, MAX_BATTLE_ROLES> _astStatRank;
    CTLib::CTArray<CBattleRolePareData, MAX_BATTLE_ROLES_PER_SIDE> _astRoleStats;
};


#endif // __CBattleRoundCtrl_h__

